package eclipse.lib.graphics;
import java.util.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;
import eclipse.lib.*;
import eclipse.lib.graphics.shaders.GLColor;
public class GLParticlePack
{
	private GLParticle[] particles;		
	private boolean[]    revive;	
	public GLParticlePack(int particleCount)
	{		
		particles = new GLParticle[particleCount];		
		revive	  = new boolean[particleCount];
	}	
	public void addParticle(GLParticle particle)
	{
		for(int i = 0; i < particles.length; i++)
		{
			if(particles[i] == null)
			{				
				particles[i] = particle;
				return;
			}			
		}
	}
	public int randomizer(int n)
	{
		Random rand = new Random();
		return rand.nextInt(Math.abs(n) + 1);
	}
	public Position randomizePosition(Position position, int variance, boolean ignoreX, boolean ignoreY, boolean ignoreZ)
	{
		Random rand = new Random();
		Position ret = new Position();		
		if(rand.nextBoolean())		
			ret.x = -ret.x;
		
		if(rand.nextBoolean())
		{
			if(!ignoreX)
				ret.x = position.x + randomizer(variance);
			else
				ret.x = position.x;
			if(!ignoreY)
				ret.y = position.y + randomizer(variance);
			else
				ret.y = position.y;
			if(!ignoreZ)
				ret.z = position.z + randomizer(variance);
			else
				ret.z = position.z;
		}
		else
		{
			if(!ignoreX)
				ret.x = position.x - randomizer(variance);
			else
				ret.x = position.x;
			if(!ignoreY)
				ret.y = position.y - randomizer(variance);
			else
				ret.y = position.y;
			if(!ignoreZ)
				ret.z = position.z - randomizer(variance);
			else
				ret.z = position.z;
		}
		
		return ret;
	}
	public void killParticle(int particleIndex)
	{
		revive[particleIndex] = false;
	}
	public void generateParticle(int total, int lifeBase, float size, Position base, int baseVar, Position friction, int frictionVar, Position velocity, int velocityVar, Position terminal, int terminalVar, Texture texture, GLColor color, boolean revive, boolean triad)
	{				
		Random rand = new Random();
		Position spinSpeed = new Position(0f, 0f, 1f);		
		for(int i = 0; i < total; i++)
		{								
			addParticle(
				new GLParticle(lifeBase + rand.nextInt(lifeBase/10) - lifeBase/10, size,
						texture, 
						randomizePosition(terminal, terminalVar, false, true, true),
						randomizePosition(velocity, velocityVar, false, true, true),
						randomizePosition(friction, frictionVar, false, true, true),
						randomizePosition(base, 	baseVar, 	 false, true, true),
						spinSpeed, 15, color));
			particles[i].triad = triad;
			this.revive[i] = revive;			
		}		
	}
	public void setParticleRevive(int particleID, boolean value)
	{
		revive[particleID] = value;
	}
	public boolean willParticleRevive(int particleID)
	{
		return revive[particleID];
	}
	public void reposition(GL gl)
	{
		for(int i = 0; i < particles.length; i++)
		{
			if(particles[i] != null)
			{				
				if(particles[i].isAlive())
					particles[i].reposition(gl);
				else	
				{										
					if(revive[i])
						particles[i].reset();
					else
						particles[i] = null;
				}
			}
		}
	}
}
